//
//  CZNewfeatureViewController.swift
//  weibo
//
//  Created by Apple on 16/3/11.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

class CZNewfeatureViewController: UIViewController {

    // MARK:    懒加载属性
    private lazy var scrollView: UIScrollView = {
        let sv = UIScrollView()
        //  设置代理
        sv.delegate = self
        //  开启分页
        sv.pagingEnabled = true
        //  不显示弹簧效果
        sv.bounces = false
        //  水平方向的滚动条不显示
        sv.showsHorizontalScrollIndicator = false
        
        return sv
    }()
    
    
    private lazy var pageControl: UIPageControl = {
        let pageCtrl = UIPageControl()
        pageCtrl.frame = CGRect(x: self.scrollView.frame.width * 0.5, y: self.scrollView.frame.height - 100, width: 0, height: 0)
        //  当前的页的颜色
        pageCtrl.currentPageIndicatorTintColor = UIColor.orangeColor()
        //  其他页的颜色是紫色
        pageCtrl.pageIndicatorTintColor = UIColor.purpleColor()
        return pageCtrl
    }()
    
    private lazy var shareButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "sharedButtonAction", forControlEvents: .TouchUpInside)
        button.setTitle("分享到微博", forState: .Normal)
        button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: -5)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setImage(UIImage(named: "new_feature_share_false"), forState: .Normal)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: .Selected)
        
        button.sizeToFit()
        return button
    }()
    private lazy var enterButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "enterButtonAction", forControlEvents: .TouchUpInside)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        button.sizeToFit()
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitle("进入主页", forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(16)
        button.sizeToFit()
        
        return button
    }()
    
    override func viewDidLoad() {

        super.viewDidLoad()
        //  指定frame
        scrollView.frame = view.bounds
        
        view.addSubview(scrollView)
        view.addSubview(pageControl)
        //  添加图片
        let count = 4
        for i in 0..<count {
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i + 1)"))
            imageView.frame = CGRect(x: CGFloat(i) * scrollView.frame.width, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)

            scrollView.addSubview(imageView)
            //  等于最后一个imageview
            if i == count - 1 {
                setLastImageViewUI(imageView)
            }
        }
        
        scrollView.contentSize = CGSize(width: CGFloat(count) * scrollView.frame.width, height: scrollView.frame.height)
        
        //  设置页数
        pageControl.numberOfPages = count
        
    }
    
    // MARK:    点击事件
    @objc private func enterButtonAction() {
        print("进入首页")
        NSNotificationCenter.defaultCenter().postNotificationName(CZSwitchRootVCNotifation, object: nil)
    }
    
    @objc private func sharedButtonAction() {
        shareButton.selected = !shareButton.selected
    }
    
    

    //  设置最后一个图片的ui
    private func setLastImageViewUI(lastImageView: UIImageView) {
        
        //  为了按钮点击有高亮效果，开启用户交互
        lastImageView.userInteractionEnabled = true
        enterButton.center = CGPoint(x: lastImageView.frame.width * 0.5, y: lastImageView.frame.height - 150)
        shareButton.center = CGPoint(x: enterButton.center.x, y: CGRectGetMidY(enterButton.frame) - 40)
        
        lastImageView.addSubview(enterButton)
        lastImageView.addSubview(shareButton)
        
    }


}
//  UIScrollViewDelegate方法
extension CZNewfeatureViewController: UIScrollViewDelegate {
    
    //  已经减速的时候开始判断
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //  获取x方向的偏移量
        let contentOffSetX = scrollView.contentOffset.x
        //  获取page索引
        let page = contentOffSetX / scrollView.frame.width
        
        pageControl.currentPage = Int(page)
    }

}
